package com.ganqiang1983.wol4j.util;

public class Host
{
  /** mac地址 **/
  private String mac;
  /** 内网广播域或者公网IP地址 **/
  private String ip;
  /** 端口号 **/
  private int port;

  public String getMac()
  {
    return mac;
  }

  public void setMac(String mac)
  {
    this.mac = mac;
  }

  public String getIp()
  {
    return ip;
  }

  public void setIp(String ip)
  {
    this.ip = ip;
  }

  public int getPort()
  {
    return port;
  }

  public void setPort(int port)
  {
    this.port = port;
  }

  /**
   * get hex byte of mac address
   * 
   * @return
   */
  public byte[] getHexMac()
  {
    return this.getMacBytes(mac.trim());
  }

  private byte[] getMacBytes(String macStr) throws IllegalArgumentException {
    byte[] bytes = new byte[6];
    String[] hex = macStr.split("(\\:|\\-)");
    if (hex.length != 6) {
      throw new IllegalArgumentException("Invalid MAC address.");
    }
    try {
      for (int i = 0; i < 6; i++) {
        bytes[i] = (byte) Integer.parseInt(hex[i], 16);
      }
    }catch (NumberFormatException e) {
      throw new IllegalArgumentException("Invalid hex digit in MAC address.");
    }
    return bytes;
  }
}